//
//  PayedVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/15.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "PayedVC.h"
#import "IntrestCell.h"
#import "PdcDetailVC.h"
//热门推荐URL
#define HOT_URL @"/api/ApiGoods/getRecommendList"
@interface PayedVC ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    //支付中提示
    UIView *whiteBg;
    
    PayedView *_payedView;
    //瀑布流
    UICollectionView *_collectionView;
    //数据源
    NSMutableArray *_data;
}
@end

@implementation PayedVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = QBB_GRAY_COLOR;
    [self loadCustomNaviBar];
    [self loadingView];
}
//等待结果
- (void)loadingView
{
    //支付中提示
    whiteBg = [[UIView alloc]initWithFrame:CGRectMake(0, 64, WYMWITH, 250)];
    whiteBg.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180, 50)];
    lab.center = CGPointMake(WYMWITH/2, whiteBg.frame.size.height/2);
    lab.text = @"订单正在支付中……";
    UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(CGRectGetMinX(lab.frame)-50, CGRectGetMinY(lab.frame), 50, 50)];
    act.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    act.center = CGPointMake(60, whiteBg.frame.size.height/2);
    [act startAnimating];
    [whiteBg addSubview:act];
    [whiteBg addSubview:lab];
    [self.view addSubview:whiteBg];
    
}
//刷新界面
- (void)updateViews
{
    _payedView = [[[NSBundle mainBundle]loadNibNamed:@"PayedView" owner:self options:nil]lastObject];
    _payedView.frame = CGRectMake(0, 64, WYMWITH, _payedView.frame.size.height);
    _payedView.father = self;
    _payedView.rltType = self.rltType;
    [self.view addSubview:_payedView];
    //加载瀑布流
    [self loadCollectionView];
    
    [whiteBg removeFromSuperview];
}
//加载导航条
- (void)loadCustomNaviBar
{
    UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 20+44)];
    naviBar.backgroundColor = [UIColor whiteColor];
    //返回按钮
    UIButton *backBtn = [UIButton buttonWithType:0];
    backBtn.frame = CGRectMake(10, 25, 30, 30);
    [backBtn setImage:[UIImage imageNamed:@"icon_back_press.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];

    [naviBar addSubview:backBtn];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 25)];
    titleLab.center = CGPointMake(WYMWITH/2, 42);
    titleLab.text = @"支付结果";
    if (self.isChongZhiFlag) {
        titleLab.text = @"充值结果";
    }
    titleLab.font = [UIFont systemFontOfSize:TITLE_SIZE];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor blackColor];
    //分割线
    [naviBar addSubview:[WYPublic getFenGexian:0 :naviBar.frame.size.height-0.5 :WYMWITH :0.5]];
    [naviBar addSubview:titleLab];
    [self.view addSubview:naviBar];
}
- (void)backBtnOnClick:(UIButton *)sender
{
    if (self.isChongZhiFlag) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
         [[WYMainTabBarVC shareMainTaBarVC].navigationController popToRootViewControllerAnimated:YES];
        if (self.rltType == PdcFailed) {
            [[WYMainTabBarVC shareMainTaBarVC]changeSelectedWithIndex:1];
        }else{
            [[WYMainTabBarVC shareMainTaBarVC]changeSelectedWithIndex:0];
        }
    }
    
}

/***********
 *加载瀑布流控件
 ************/
- (void)loadCollectionView
{
    //初始化瀑布流
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_payedView.frame), WYMWITH, 110*app.autoSizeScaleY) collectionViewLayout:layout];
    //设置代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //注册cell
    UINib *nib = [UINib nibWithNibName:@"IntrestCell" bundle:nil];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    
    _data = [[NSMutableArray alloc]initWithArray:[[WYDataBase sharedDataBase]getAllpdcInIntrestCar]];
    if (_data.count > 0) {
        [_collectionView reloadData];
        if (_data.count < 10) {
            [self startRequestForData];
        }
    }else{
        [self startRequestForData];
    }
    
}
//请求网络数据
- (void)startRequestForData
{
    NSDictionary *params = @{@"limit":@"10"};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,HOT_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSArray *pdcs = dic[@"data"];
        for (NSDictionary *pdc in pdcs) {
            WYShopCarModel *model = [[WYShopCarModel alloc]init];
            model.car_id = pdc[@"id"];
            model.car_new_id = pdc[@"sid"];
            model.name = pdc[@"title"];
            model.imgPath = pdc[@"thumb_url"];
            [_data addObject:model];
            //保存
            [[WYDataBase sharedDataBase]insertPdcToIntrestCarWithModel:model];
            [_collectionView reloadData];
        }
        [_collectionView reloadData];
    } failedBlock:^{
        
    } needLog:NO];
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    return CGSizeMake(120*myDelegate.autoSizeScaleX, 110*myDelegate.autoSizeScaleY);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _data.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IntrestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (_data.count>0) {
        //获取当前数据模型
        WYShopCarModel *model = _data[indexPath.row];
        //填充cell
        [cell fillCellWithModel:model];
    }
    //    cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return cell;
}
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //保存至兴趣记录
    WYShopCarModel *model = _data[indexPath.row];
    model.count = 1;
    [[WYDataBase sharedDataBase]insertPdcToIntrestCarWithModel:model];
    _data = [[NSMutableArray alloc]initWithArray:[[WYDataBase sharedDataBase]getAllpdcInIntrestCar]];
    [_collectionView reloadData];
    
    //跳往商品详情
    PdcDetailVC *detailVC = [[PdcDetailVC alloc]init];
    detailVC.pdc_id = model.car_id;
    detailVC.isSidFlag = YES;
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:detailVC animated:YES];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    WYGoodsModel *model = _data[indexPath.row];
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
